var $pref ={
	str:function(key,val){
		var obj=key;
		if ( val == undefined || key.match(/^\?/)){
			if(key.match(/^!/)){
				this.mPrefService.clearUserPref(key.replace(/^!/,''));
				return;
			}
			
			switch (this.mPrefService.getPrefType(key)){
				case 32:
					return this.getChar(key.replace(/^\?/,''),val);
					break;
				case 64:
					return this.getInt(key.replace(/^\?/,''),val);
					break;
				case 128:
				default:
					return this.getBool(key.replace(/^\?/,''),val);
					break;
			}
			return;
		}else {
			obj = {};
			obj[ key ] = val;
			this.log(key+val+typeof(val))
		}
		for (var key in obj){
			switch (typeof(obj[key])){
				case 'boolean':
						this.setBool(key,obj[key]);
					//	return this.getBool(key);
					break;
				case 'number':
						this.setInt(key,obj[key]);
				//		return this.getInt(key);
					break;
				default:
					this.setChar(key,obj[key]);
				//	return this.getChar(key);
			}
		}
	},
	get mPrefService() {
		return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	},
	setBool: function (aPrefName, aPrefValue){
		try {
			this.mPrefService.setBoolPref(aPrefName, aPrefValue);
		}catch(e){}
	},

	getBool: function (aPrefName, aDefVal) {
		try {
			return this.mPrefService.getBoolPref(aPrefName);
		}catch(e){
			return aDefVal != undefined ? aDefVal : null;
		}
		return null;
	},

	setChar: function (aPrefName, aPrefValue){
		try{
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			str.data = aPrefValue;
			this.mPrefService.setComplexValue(aPrefName,Components.interfaces.nsISupportsString, str);
		}catch(e){}
	},

	setInt: function (aPrefName, aPrefValue){
		try{
			this.mPrefService.setIntPref(aPrefName, aPrefValue);
		}catch(e){ }
	},

	getInt: function (aPrefName, aDefVal) {
		try{
			return this.mPrefService.getIntPref(aPrefName);
		}catch(e){
			return aDefVal != undefined ? aDefVal : null;
		}
		return null;
	},

	getChar: function (aPrefName, aDefVal) {
		try{
			return this.mPrefService.getComplexValue(aPrefName,Components.interfaces.nsIPrefLocalizedString).data;
		}catch(e){
			return aDefVal != undefined ? aDefVal : null;
		}
		return null;
	},
	log: function(str) {
		Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService).logStringMessage(str);
	},
	elm:{
		from:function(){
			if(!$elm){return;}
			var pref=$elm.get({"pref":"*"});
			for (var i=0;i<pref.length;i++){
				var val=pref[i].value;
				if(pref[i].value=="true"){val=true;}
				if(pref[i].value=="false"){val=false}
				if(/[0-9]+/i.test(pref[i].value)){var val=parseInt(pref[i].value);}
				$pref.str(pref[i].getAttribute("pref"),val);
			}
		},
		to:function(a){
			if(!$elm){return;}
			var prefs=$elm.get("pref");
			for (var i=0;i<prefs.length;i++){
				var key=prefs[i].getAttribute("key")
				var val=$pref.str(key)
				var pref=document.getElementsByAttribute("pref",key)
				if(!pref.length){continue;}
				for (var ii=0;ii<pref.length;ii++){
					pref[ii].value=val
				}
			}
		},
	},
};
